﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Dude.MLS;
using System.ComponentModel;

namespace Dude.Data.Meta
{
    [Serializable]
    [XmlRoot(ElementName = "database")]
    public class DatabaseInfo : ITreeReconstruct, IVisitedDataModel
    {
        public DatabaseInfo()
        {
            //Aliases = new List<string>();
            Tables = new List<TableInfo>();
            DisplayName = new MLString();
            Description = new MLString();
            LongDescription = new MLString();
        }

        [Category("General")]
        [XmlAttribute("name")]
        public string DatabaseName { get; set; }

        [Category("General")]
        [XmlElement("alias")]
        public string[] Aliases { get; set; }

        [Category("General")]
        [XmlAttribute("connection")]
        public string ConnectionName { get; set; }

        [Category("Localization")]
        [XmlElement("display")]
        public MLString DisplayName { get; set; }

        [Category("Localization")]
        [XmlElement("desc")]
        public MLString Description { get; set; }

        [Category("Localization")]
        [XmlElement("longdesc")]
        public MLString LongDescription { get; set; }

        [Category("Database tables")]
        [XmlElement("table")]
        public List<TableInfo> Tables { get; set; }

        [Browsable(false)]
        [XmlIgnore]
        public DataModel DataModel { get; set; }

        #region ITreeReconstruct Members

        public void ReconstructTree(object parent)
        {
            DataModel = (DataModel)parent;
            foreach (ITreeReconstruct r in Tables)
            {
                if (r != null)
                    r.ReconstructTree(this);
            }
        }

        #endregion

        public override string ToString()
        {
            return DatabaseName;
        }

        #region IVisitedDataModel Members

        public void VisitedDataModel(IDataModelVisitor visitor)
        {
            visitor.VisitDatabaseInfo(this);
        }

        #endregion
    }
}
